package lu.med.praxiswahl.wahlumed.client.laborbuch;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;
import lu.med.praxiswahl.wahlumed.client.utilities.ColumnWidthControler;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;

/**
 * 
 * @author Lars Holger Roth
 */
public class LaborbuchTable extends JTable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 575315479264301276L;

	private ColumnWidthControler c;
	private boolean isColumnWidthChanged;
	private TableColumn resizedColumn;

	public LaborbuchTable(LaborbuchDataModel model) {
		super(model);
		this.setRowHeight(30);
		this.setTableHeader(tableHeader);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.setAutoResizeMode(AUTO_RESIZE_OFF);
		this.selectionModel = getSelectionModel();
		this.selectionModel
				.addListSelectionListener(new LaborbuchTableSelectionListener(
						model));
		this.setDefaultRenderer(Object.class, new LaborbuchCellRenderer(model));
		this.setDefaultEditor(Object.class,
				new LaborbuchCellEditor(model, null));

		TableColumn column = null;

		c = new ColumnWidthControler(Main.COLUMNWIDTH_CONFIGURATION_FILE,
				"laborbooktbl", this);

		for (int i = 0; i < 7; i++) {
			column = this.getColumnModel().getColumn(i);
			column.setPreferredWidth(c.getColumnWidth(i));
		}
		this.getColumnModel().addColumnModelListener(
				new TableColumnWidthListener(this));
		this.getTableHeader().addMouseListener(
				new TableHeaderMouseListener(this, c));
	}

	public boolean getColumnWidthChanged() {
		return isColumnWidthChanged;
	}

	public void setColumnWidthChanged(boolean widthChanged,
			TableColumn resizedColumn) {
		isColumnWidthChanged = widthChanged;
		this.resizedColumn = resizedColumn;
	}

	public TableColumn getResizedColumn() {
		return resizedColumn;
	}

	@Override
	public void changeSelection(final int row, final int column,
			boolean toggle, boolean extend) {
		super.changeSelection(row, column, toggle, extend);
		editCellAt(row, column);
		transferFocus();
	}
}
